package data

import (
	"blog/api/global"
	"blog/internal/common"
	"blog/internal/constx"
	gen "blog/internal/ent"
	"blog/internal/ent/files"
	"context"
	"encoding/json"
	"fmt"
	"github.com/alibabacloud-go/sts-20150401/v2/client"
	"time"

	"blog/app/tools/internal/biz"

	"github.com/go-kratos/kratos/v2/log"
)

type toolsRepo struct {
	data *Data
	log  *log.Helper
}

// NewToolsRepo .
func NewToolsRepo(data *Data, logger log.Logger) biz.ToolsRepo {
	return &toolsRepo{
		data: data,
		log:  log.NewHelper(logger),
	}
}

func (r *toolsRepo) SaveFile(ctx context.Context, g *biz.File) error {
	return r.data.db.Files.Create().
		SetID(g.ID).SetName(g.Name).SetType(g.Type).SetSize(g.Size).SetPath(g.Path).
		OnConflict().DoNothing().Exec(ctx)
}
func (r *toolsRepo) ExistFile(ctx context.Context, g *biz.File) (string, error) {
	f, err := r.data.db.Files.Query().Where(files.IDEQ(g.ID)).Only(ctx)
	if err != nil {
		if gen.IsNotFound(err) {
			return "", nil
		}
		return "", err
	}
	return f.Path, nil
}
func (r *toolsRepo) FindFile(ctx context.Context, g *biz.File) (*biz.File, error) {
	file, err := r.data.db.Files.Get(ctx, g.ID)
	if err != nil {
		return nil, err
	}
	return &biz.File{
		ID:   file.ID,
		Type: file.Type,
		Size: file.Size,
		Name: file.Name,
		Path: file.Path,
	}, nil
}
func (r *toolsRepo) CacheFile(ctx context.Context, origin, path string, expire int64) {
	if r.data.redisCli == nil {
		return
	}
	r.data.redisCli.Set(ctx, constx.FileCachePrefix+origin, path, time.Duration(expire)*time.Minute)
}

func (r *toolsRepo) FilesInfo(ctx context.Context, path []string) ([]*biz.File, error) {
	fs, err := r.data.db.Files.Query().Where(files.IDIn(path...)).All(ctx)
	if err != nil {
		return nil, err
	}
	fileList := make([]*biz.File, len(fs))
	for i, f := range fs {
		fileList[i] = &biz.File{
			ID:   f.ID,
			Type: f.Type,
			Size: f.Size,
			Name: f.Name,
			Path: f.Path,
		}
	}
	return fileList, nil
}
func (r *toolsRepo) OssToken(ctx context.Context, cfg *global.Oss, crets *client.AssumeRoleResponseBodyCredentials, isSave bool) error {
	by, _ := json.Marshal(cfg)
	key := fmt.Sprintf("%soss-token:%s:%s", constx.FileCachePrefix, cfg.Arn, common.MD5(string(by)))
	if isSave {
		by, _ = json.Marshal(crets)
		return r.data.redisCli.Set(ctx, key, string(by), time.Duration(cfg.Expire-300)*time.Second).Err()
	}
	data := r.data.redisCli.Get(ctx, key).String()
	return json.Unmarshal([]byte(data), crets)
}
